<template>
  <div>
    <div class="crumbs">
      <router-link to="/assets/wait">
        <el-button type="success">未审核</el-button>
      </router-link>
      <router-link to="/assets/list">
        <el-button>审核通过</el-button>
      </router-link>
      <router-link to="/assets/failed">
        <el-button>审核失败</el-button>
      </router-link>
    </div>
    <div class="crumbs">
      <el-form ref="form" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input v-model="formInline.keywords" placeholder="请输入……" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch('form')">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" border style="width: 100%" :row-key="'aa'">
      <el-table-column prop="id" label="ID"  width="100"></el-table-column>
      <el-table-column prop="account" label="持卡人"  width="120"></el-table-column>
      <el-table-column prop="name" label="发行卡名称"  width="200"></el-table-column>
      <el-table-column prop="number" label="发行卡号"  width="300"></el-table-column>
      <el-table-column prop="create_time" label="创建时间"  width="150"></el-table-column>
      <el-table-column prop="status_text" label="状态" width="150"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="see(scope.$index, scope.row)">查看</el-button>
          <el-button v-if="scope.row.status === 0" size="small" @click="pass(scope.$index, scope.row)">审核通过</el-button>
          <el-button v-if="scope.row.status === 0" size="small" @click="failed(scope.$index, scope.row)">审核失败</el-button>
          <el-button size="small" @click="del(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change ="handleCurrentChange"
        layout="total, prev, pager, next"
        :current-page="textData.current_page"
        :page-size="textData.per_page"
        :total="textData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data () {
      return {
        formInline: {
          keywords: ''
        }
      }
    },
    computed: {
      ...mapState({
        tableData: state => state.bank.list,
        textData: state => state.bank.pageData
      })
    },
    created () {
      this.getBankCardList()
    },
    methods: {
      async getBankCardList () {
        let payload = {
          status: 0,
          page: this.textData.current_page,
          list_rows: this.textData.per_page,
          keywords: this.$route.query.keywords
        }
        await this.$store.dispatch('bank/cardList', payload)
      },
      onSearch (formName) {
        const self = this
        self.$refs[formName].validate((valid) => {
          if (valid) {
            self.$router.push({path: '/assets/list', query: {keywords: self.formInline.keywords}})
            self.getBankCardList()
          }
        })
      },
      handleCurrentChange (val) {
        this.textData.current_page = val
        this.getBankCardList()
      },
      see (index, row) {
        this.$store.commit('bank/BANK_CARD_SAVE_DATA', row)
        this.$router.push('/assets/info')
      },
      pass (index, row) {
        let payload = {
          id: row.id,
          status: 1
        }
        this.$store.dispatch('bank/audit', payload).then(res => {
          this.getBankCardList()
          notify.success(res.msg)
        })
      },
      failed (index, row) {
        let payload = {
          id: row.id,
          status: 2
        }
        this.$store.dispatch('bank/audit', payload).then(res => {
          this.getBankCardList()
          notify.success(res.msg)
        })
      },
      del (index, row) {
        let payload = {
          id: row.id
        }
        this.$store.dispatch('bank/delCard', payload).then(res => {
          this.getBankCardList()
          notify.success(res.msg)
        })
      }
    }
  }
</script>

<style scoped>
  .crumbs {
    display: inline-block;
    vertical-align: top;
    margin-right: 50px;
  }
</style>
